local EntUtil = {}

function EntUtil:isValid(ent)
    return ent and ent.Transform and ent.entity
end

function EntUtil:getContainer(ent)
    if ent and ent.replica then
        return ent.replica.container or ent.replica.inventory
    end
end


function EntUtil:hashMatching(ent, handletable, hashvalue)
    local prefab = ent and ent.prefab
    if type(handletable) == "table" and prefab then
        for _,v in pairs(handletable)do
            local tex = v..".tex"
            if hash(tex) == hashvalue then
                return tex
            end
        end
    end
end

function EntUtil:getStacksize(ent)
    if ent and ent.replica and ent.replica.stackable then
        return ent.replica.stackable:StackSize()
    end
    return 1
end

require("prefabskins")
function EntUtil:getImage(ent)
    if ent and ent.replica and ent.replica.inventoryitem then
        local img = ent.replica.inventoryitem:GetImage()
        local prefab = ent.prefab
        if type(img) == "number" and prefab then
            local tex
            if ent:HasTag("bundle") then
                local tails = {"_small", "_medium", "_large", "_small1", "_medium1", "_large1", "_small2", "_medium2", "_large2"}
                local handletable = {}
                for _,tail in pairs(tails)do
                    table.insert(handletable, prefab..tail)
                end
                tex = self:hashMatching(ent, handletable, img)
            else
                tex = self:hashMatching(ent, PREFAB_SKINS and PREFAB_SKINS[prefab], img)
            end
            if tex then
                return tex
            end
        end
        return img ~= "" and type(img)~="number" and img or prefab..".tex"
    end
end


function EntUtil:getAtlasAndImage(ent)
    local item = ent and ent.replica and ent.replica.inventoryitem
    if item then
        return item:GetAtlas(), item:GetImage()
    end
end

return EntUtil